from PySide6.QtWidgets import QApplication, QMainWindow, QGraphicsScene, QGraphicsView
from PySide6.QtCore import QTimer
from PySide6.QtGui import QPainterPath, QPen, QColor, QFont
from PySide6.QtCore import Qt, QPointF
from collections import deque

import math

from demo_ui import Ui_MainWindow   # 导入生成的界面类

from PySide6.QtWidgets import QGraphicsScene
from PySide6.QtGui import QPen, QPainter
from PySide6.QtCore import Qt

class CustomScene(QGraphicsScene):
    def __init__(self, parent=None):
        super().__init__(parent)

    def drawBackground(self, painter, rect):
        super().drawBackground(painter, rect)

        # 设置坐标轴的画笔
        axis_pen = QPen(Qt.black, 2)
        painter.setPen(axis_pen)

        # 绘制坐标轴
        painter.drawLine(rect.left(), 0, rect.right(), 0)  # X轴
        painter.drawLine(0, rect.top(), 0, rect.bottom())  # Y轴

        # 设置刻度线的画笔
        gray_color = QColor(128, 128, 128)      # 灰色
        tick_pen = QPen(gray_color)             # 灰色画笔
        tick_pen.setStyle(Qt.DashLine)          # 虚线
        tick_pen.setWidth(0.5)                  # 线宽
        painter.setPen(tick_pen)                # 设置画笔

        # 绘制X轴刻度线

        tick_size = 50
        for x in range(-1000, 1000, tick_size):     # 间隔50绘制刻度线
            painter.drawLine(x, -1000, x, 1000)  # 刻度线长度为10
            if x%100 == 0:
                painter.drawText(x, 0, str(x))       # 绘制刻度值

        # 绘制Y轴刻度线
        for y in range(-1000, 1000, tick_size):         # 间隔50绘制刻度线
            painter.drawLine(-1000, y, 1000, y)  # 刻度线长度为10
            painter.drawText(0, y, str(y))       # 绘制刻度值



class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        self.ui = Ui_MainWindow()   # 创建界面类实例
        self.ui.setupUi(self)       # 构建界面
        
        # 设置图形场景和视图
        self.scene = CustomScene()                      # 创建自定义场景
        self.view = QGraphicsView(self.scene, self)     # 创建视图


        self.view_height = 500          # 视图高度
        self.view_width = 800           # 视图宽度
        self.view.setFixedHeight(self.view_height)      # 固定视图高度
        self.view.setFixedWidth(self.view_width)        # 固定视图宽度
        self.view.move(10, 50)                    # 移动视图

        # 初始化绘制路径
        self.path = QPainterPath()      # 创建绘制路径
        self.path.moveTo(0, 0)          # 移动到原点

        # 定时器用于更新曲线
        self.timer = QTimer(self)    # 创建定时器
        self.timer.timeout.connect(self.update_curve) # 连接信号槽
        self.timer.start(10)  # 更新间隔（毫秒）

        self.maxRange = self.view_width - 10    # 最大范围

        self.y_array = [0] * (self.maxRange + 1) # 创建数组
        self.x = 0

        


    def update_curve(self):
        # 计算新点
        y = math.sin(math.radians(self.x)) * 150    # 计算y值

        self.y_array[self.maxRange] = y   # 存储y值

        self.path = QPainterPath()      # 创建绘制路径

        if self.y_array:        # 如果数组不为空
            self.path.moveTo(0, self.y_array[0])        # 移动到原点
            for i, point in enumerate(self.y_array):    # 遍历数组
                self.path.lineTo(i, point)          # 添加点


        # 清除场景并添加新路径
        self.scene.clear()  # 清除旧的图形项    
        self.scene.addPath(self.path, QPen(Qt.black, 2))    # 添加新的路径

        for i in range(self.maxRange):  # 遍历数组
            self.y_array[i] = self.y_array[i+1]     # 移动数组元素
        
        self.x += 1


if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()
